<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/5/22
 * Time: 下午5:42
 */

namespace app\index\model;


use think\Model;

class SubCategory extends Model
{
    protected $table = 't_secqualify_category';
    public static function getCategoryNameById($id){
        $cateogory = self::find($id);
        return $cateogory['category_name'];

    }
    public static function getSubCategoryByPid($pid){
        $data = self::where("parent_cate_id","=","$pid")->select();
        if($data->isEmpty()){
            return [];
        }
        $subcategory = [];
        $count = 0;
        foreach ($data as $item ){
            $category_item["id"] = $item["id"];
            $category_item["category_name"] = $item["category_name"];
            array_push($subcategory,$category_item);
//            $count++;
//            if($count == 9){
//                break;
//            }
        }
        return $subcategory;

    }
    public static function getLimitSubCategoryByPid($pid,$limit=9){
        $data = self::where("parent_cate_id","=","$pid")->select();
        if($data->isEmpty()){
            return [];
        }
        $subcategory = [];
        $count = 0;
        foreach ($data as $item ){
            $category_item["id"] = $item["id"];
            $category_item["category_name"] = $item["category_name"];
            $category_item["pid"] = $pid;
            array_push($subcategory,$category_item);
            $count++;
            if($count == $limit){
                break;
            }
        }
        return $subcategory;

    }
    public static function getCategoryById($id){
        $data = self::find($id);
        return $data;
    }
    public static function getChildCategorysByPid($parent_cate_id){
        $sub_category_list = [];
        $data = self::where("parent_cate_id","=",$parent_cate_id)
            ->where("flag","=",1)->order("show_order asc")->limit(0,4)->select();

        foreach ($data as $item){
            $cate_item["id"] = $item["id"];
            $cate_item["category_name"] = $item["category_name"];
            array_push($sub_category_list,$cate_item);
        }
        return $sub_category_list;


    }
    public static function getAll(){
        $data = self::where('flag', '=', 1)
            ->order('show_order desc')
            ->select();
        if($data->isEmpty()){
            return[];
        }
        $subcategory = [];
        foreach ($data as $item){
            $category_item['id'] = $item['id'];
            $category_item['category_name'] = $item['category_name'];
            array_push($subcategory,$category_item);
        }
        return $subcategory;
    }
    public static function getSupCateIdById($id){
        $data = self::find($id);
        if($data){
            return $data['parent_cate_id'];
        }else{
            return "";
        }

    }

}